//
//  PopoverAnimator.swift
//  swift_wobo
//
//  Created by yuxin on 2018/11/17.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit


// 定义常量保存通知的名称
let YXPopoverAnimatorWillChange = "YXPopoverAnimatorWillChange"
let YXPopoverViewStatuskey = "YXPopoverViewStatuskey"


class PopoverAnimator: NSObject {
    
//    typealias swiftBlock = (_ presented : Bool) -> ()
    // MARK:- 对外提供的属性
    var isPresented : Bool = false
    var presentFrame : CGRect = CGRect.zero
    
    override init() {
        
    }
    
}

// MARK: - UIViewControllerTransitioningDelegate
extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    // 返回负责转场的控制器对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let popoverController = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        
        return popoverController
    }
    
     //目的：自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: YXPopoverAnimatorWillChange), object: nil, userInfo: [ YXPopoverViewStatuskey : "show" ])
        return self
    }
   
    
    //目的：自定义消失出动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: YXPopoverAnimatorWillChange), object: nil, userInfo: [ YXPopoverViewStatuskey : "dismiss" ])
        return self
    }
    
}

//MARK: - 弹出和消失的动画方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    
    //设置动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForShow(transitionContext: transitionContext) : animationForDismiss(transitionContext: transitionContext)
    }
    
    
    //自定义弹出动画
    private func animationForShow(transitionContext: UIViewControllerContextTransitioning) {
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        transitionContext.containerView.addSubview(presentedView!)
        
        //动画
        presentedView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        presentedView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        let duration : TimeInterval = transitionDuration(using: transitionContext)
        UIView.animate(withDuration: duration, animations: {
            presentedView?.transform = CGAffineTransform.identity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    //自定义隐藏动画
    private func animationForDismiss(transitionContext: UIViewControllerContextTransitioning) {
        let dismissView = transitionContext.view(forKey: .from)
        
        let duration : TimeInterval = transitionDuration(using: transitionContext)
        UIView.animate(withDuration: duration, animations: {
            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    
}
